package com.yc.service;
import com.google.gson.Gson;
import com.yc.domain.MessageBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.concurrent.Executor;

/**
 * @author zwei
 * @create 2024-08-11 16:25
 */
@Component  // 注入到spring容器中
@Slf4j
public class JmsMessageProducer {
    @Autowired
    private JmsTemplate jmsTemplate;
    @Autowired
    private Executor taskExecutor;
    @Async("taskExecutor")
    public void sendMessage(MessageBean messageBean) {
        //将bean转成一个json字符串   序列化到 activeMQ
        Gson gson = new Gson();
        String json = gson.toJson(messageBean);
        log.info("发送的消息为：" + json);
        jmsTemplate.convertAndSend("好吃点餐网", json);    //发送消息到队列
    }
}
